/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package panel;

import database.CompanyInfo;
import database.DataEngine;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

/**
 *
 * @author daz
 */
public class CpInfoPanel extends Screen {

    private JLabel cpNameLabel;
    private JTextField cpNameInput;
    private JButton showInfoButton;
    private ActionListener showListenner;
    private DataEngine dataEngine = DataEngine.getInstance();
    private JLabel infoLabel;
    private JTextArea infoArea;

    public CpInfoPanel(JFrame frame) {
        super(frame);
    }

    @Override
    void initValue() {
    }

    @Override
    void initComponent() {
        //cp line
        setLayout(null);
        int left = 10;
        int top = 20;
        int height = 20;
        int width = 250;
        int linePadding = 35;
        cpNameLabel = new JLabel("Tên cp :");
        cpNameLabel.setBounds(left + 30, top, 250, height);
        add(cpNameLabel);
        cpNameInput = new JTextField();
        cpNameInput.setBounds(left + 130, top, width, height);
        add(cpNameInput);
        // database user line
        top += linePadding;
        infoLabel = new JLabel("Thông tin:");
        infoLabel.setBounds(left + 30, top, 250, height);
        add(infoLabel);

        infoArea = new JTextArea();
        infoArea.setBounds(left + 130, top, width, 300);
        add(infoArea);

        top += linePadding;
        showInfoButton = new JButton("Xem thông tin");
        add(showInfoButton);
        showInfoButton.addActionListener(showListenner);
        showInfoButton.setBounds(left, top, 100, height);
    }

    @Override
    void initListenner() {
        showListenner = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (cpNameInput.getText().length() == 0) {
                    showInfo("Chưa điền tên cp");
                    return;
                }
                CompanyInfo companyInfo = dataEngine.getCpInfo(cpNameInput.getText());
                if (companyInfo != null) {
                    infoArea.setText("");
                    infoArea.append("id :" + companyInfo.getCompanyId() + "\n");
                    infoArea.append("cú pháp :" + companyInfo.getSynTax() + "\n");
                    infoArea.append("Đầu số kích hoạt :" + companyInfo.getNumberActive() + "\n");
                    infoArea.append("Đầu số nạp tiền :" + companyInfo.getNumberCharge1() + "\n");
                    infoArea.append("Đầu số nạp tiền :" + companyInfo.getNumberCharge2() + "\n");
                    infoArea.append("Đầu số nạp tiền :" + companyInfo.getNumberCharge3() + "\n");
                    infoArea.append("Hệ số nạp tiền :" + companyInfo.getCoefficient() + "\n");
                } else {
                    showInfo("cp " + cpNameInput.getText() + " không tồn tại");
                }
            }
        };
    }

    private void showInfo(String info) {
        JOptionPane.showMessageDialog(frame,
                info);
    }
}
